package com.test.design_pattern.proxy.demo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy implements InvocationHandler {
    private Object target;//需要代理的目标对象



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("JDK动态代理，监听开始！");
        Object result = method.invoke(target, args);
        System.out.println("JDK动态代理，监听结束！");
        return result;
    }

    public Object getProxyObject(Object o){
        this.target = o;
        Object res = Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), this);
        return res;
    }

    public static void main(String[] args) {
        JDKProxy userManagerJDKProxy = new JDKProxy();
        UserManager userManager = new UserManagerImpl();
        UserManager um = (UserManager) userManagerJDKProxy.getProxyObject(userManager);
        um.addUser("zs", "123");
    }
}